package com.giw.project.genre_managment;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.giw.project.exception_manager.UncheckedException;

public class RetrieveOriginalMoviePages {

	private final static String movies_nogenre_path="/Users/macbook/Desktop/IngegneriaInformatica" +
	"/GIW/Progetto_Finale/GenreManagment/moviewithoutgenre";
	private final static String N3filepath="/Users/macbook/Desktop/IngegneriaInformatica/GIW/" +
	"Progetto_Finale/inputN3/linkedmdb-latest-dump";
	private final static String movies_nogenre_done="/Users/macbook/Desktop/IngegneriaInformatica" +
	"/GIW/Progetto_Finale/GenreManagment/movieDone";
	private List<String> movieDone;

	public RetrieveOriginalMoviePages(){
		this.movieDone=new LinkedList<String>();
		FileReader f;
		try {
			f = new FileReader(movies_nogenre_done);
		} catch (FileNotFoundException e) {
			throw new UncheckedException("Path of movies without genre not found.\nMessage: "+e.getMessage());
		} 
		BufferedReader in =new BufferedReader(f);
		String line;
		try {
			line = in.readLine();
			while(line!=null){
				this.movieDone.add(line);
				line=in.readLine();
			}
			in.close();
			f.close();
		}catch(IOException e){
			throw new UncheckedException("Error while reading the file with movies" +
					"without genre.\nMessage: "+e.getMessage()); 
		}

	}


	private List<String> getUrlGenre(){
		List<String> movies=new LinkedList<String>();
		FileReader f;
		try {
			f = new FileReader(movies_nogenre_path);
		} catch (FileNotFoundException e) {
			throw new UncheckedException("Path of movies without genre not found.\nMessage: "+e.getMessage());
		} 
		BufferedReader in =new BufferedReader(f);
		String line;
		try {
			line = in.readLine();
			while(line!=null){
				if(!this.movieDone.contains(line)){
					movies.add(line);
				}
				line=in.readLine();
			}
			in.close();
			f.close();
		}catch(IOException e){
			throw new UncheckedException("Error while reading the file with movies" +
					"without genre.\nMessage: "+e.getMessage()); 
		}
		if(movies.size()==0) return null;
		return movies;
	}
	public Map<String,String> getOriginalPage(){
		List<String> links=this.getUrlGenre();
		if(links==null) return null;
		Map<String,String> new_links=new HashMap<String,String>();
		FileReader f;
		String n_link;
		String o_link;
		try {
			f = new FileReader(N3filepath);
		} catch (FileNotFoundException e) {
			throw new UncheckedException("Path of N3 file not found.\nMessage: "+e.getMessage());
		} 
		BufferedReader in =new BufferedReader(f);
		String line;
		try {
			line = in.readLine();
			while(line!=null){
				if(line.matches("<http://data.linkedmdb.org/resource/film/[0-9]*> " +
				"<http://xmlns.com/foaf/0.1/page> <http://www.freebase.*")){
					if(links.contains(line.split(" ")[0])){
						n_link=line.split(" ")[2];
						n_link=n_link.replace("<","").replace(">","");
						o_link=line.split(" ")[0];
						o_link=o_link.replace("<","").replace(">","");
						new_links.put(o_link,n_link);
					}
				}
				line=in.readLine();
			}
		}catch(IOException e){
			throw new UncheckedException("Error while reading the N3 file." +
					"\nMessage: "+e.getMessage()); 
		}

		if(new_links.size()==0) return null;
		return new_links;

	}


	public static void main(String args[]){
		RetrieveOriginalMoviePages c=new RetrieveOriginalMoviePages();
		Map<String,String> l2=c.getOriginalPage();
		if(l2==null) System.out.println("NO!");
		else System.out.print(l2.toString());
	}

}
